package com.mazaiting.rtda;

/**
 * 局部变量表
 *
 * @author mazaiting
 * @since 2021/8/13
 */
public class MtLocalVars {
    /**
     * 局部变量表类型
     */
    private MtSlot[] slots;

    public MtLocalVars(int maxLocals) {
        if (maxLocals > 0) {
            this.slots = new MtSlot[maxLocals];
        }
    }

    /**
     * 设置整型值
     * @param index 索引
     * @param value 值
     */
    public void setInt(int index, int value) {
        slots[index] = new MtSlot();
        slots[index].setNum(value);
    }

    /**
     * 获取整型值
     * @param index 索引
     * @return 整型值
     */
    public int getInt(int index) {
        return slots[index].getNum();
    }

    /**
     * 设置单精度值
     * @param index 索引
     * @param value 值
     */
    public void setFloat(int index, float value) {
        slots[index] = new MtSlot();
        slots[index].setNum(Float.floatToIntBits(value));
    }

    /**
     * 获取单精度值
     * @param index 索引
     * @return 单精度值
     */
    public float getFloat(int index) {
        return Float.intBitsToFloat(slots[index].getNum());
    }

    /**
     * 设置long值
     * @param index 索引
     * @param value 值
     */
    public void setLong(int index, long value) {
        slots[index] = new MtSlot();
        slots[index+1] = new MtSlot();
        slots[index].setNum((int)value);
        slots[index+1].setNum((int)(value >> 32));
    }

    /**
     * 获取long值
     * @param index 索引
     * @return long值
     */
    public long getLong(int index) {
        int lower = slots[index].getNum();
        int high = slots[index+1].getNum();
        return (((long)high << 32) & 0xFFFFFFFF00000000L) | ((long)lower & 0xFFFFFFFFL);
    }

    /**
     * 设置double值
     * @param index 索引
     * @param value 值
     */
    public void setDouble(int index, double value) {
        long longBits = Double.doubleToLongBits(value);
        setLong(index, longBits);
    }

    /**
     * 获取double值
     * @param index 索引
     * @return double 值
     */
    public double getDouble(int index) {
        long value = getLong(index);
        return Double.longBitsToDouble(value);
    }

    /**
     * 设置引用
     * @param index 索引
     * @param value 引用
     */
    public void setRef(int index, MtObject value) {
        slots[index] = new MtSlot();
        slots[index].setRef(value);
    }

    /**
     * 获取引用
     * @param index 索引
     * @return double 值
     */
    public MtObject getRef(int index) {
        return slots[index].getRef();
    }

}


















